package com.whr.service;

import com.whr.bean.Columns;
import com.whr.bean.Resources;
import com.whr.dao.ResourcesMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Transactional
@Service("resService")
public class ResServiceImpl implements ResService {
    @Resource
    private ResourcesMapper resourcesMapper;

    @Override
    public List<Resources> findAll() {
        return resourcesMapper.selectAll();
    }

    @Override
    public void saveRes(Resources res) {
        String rparent = res.getRparent();
        String rname = res.getRname();
        int k = 1;
        if (!StringUtils.isEmpty(rparent)) {

            for (int i = 0; ; i += 2) {
                if (rparent.startsWith("……", i)) {
                    ++k;
                } else {
                    break;
                }
            }
            for (int j = 0; j < k; j++) {
                rname = "……" + rname;
            }
        }
        res.setRname(rname);
        res.setRstate("启用");
        resourcesMapper.insert(res);
    }

    @Override
    public void stop(Integer[] checks) {
        resourcesMapper.stopRes(checks);
    }

    @Override
    public void start(Integer[] checks) {
        resourcesMapper.startRes(checks);
    }

    @Override
    public List<String> selMenu() {
        return resourcesMapper.selMenu();
    }

    @Override
    public Resources findById(Integer checks) {
        Resources res0 = resourcesMapper.selectByPrimaryKey(checks);
        String rname0 = res0.getRname();
        String rname = rname0.replaceAll("……", "");
        res0.setRname(rname);
        return res0;
    }

    @Override
    public void updateRes(Resources res) {
        String rparent = res.getRparent();
        String rname = res.getRname();
        int k = 1;
        if (!StringUtils.isEmpty(rparent)) {
            for (int i = 0; ; i += 2) {
                if (rparent.startsWith("……", i)) {
                    ++k;
                } else {
                    break;
                }
            }
            for (int j = 0; j < k; j++) {
                rname = "……" + rname;
            }
        }
        res.setRname(rname);
        resourcesMapper.updateByPrimaryKey(res);
    }

    @Override
    public void delRes(Integer[] checks) {
        resourcesMapper.delRes(checks);
    }

    @Override
    public List<Columns> findCol() {
        return resourcesMapper.findCol();
    }

    @Override
    public void vbl(Integer[] checks) {
        resourcesMapper.vbl(checks);
    }

    @Override
    public void unvbl(Integer[] checks) {
        resourcesMapper.unvbl(checks);
    }

    @Override
    public Columns findColById(Integer cid) {
        return resourcesMapper.findColById(cid);
    }

    @Override
    public void updateCol(Columns column) {
        resourcesMapper.updateCol(column);
    }

    @Override
    public List<Columns> findColAll() {
        return resourcesMapper.findColAll();
    }

    @Override
    public List<Resources> findMenuByPath(String path) {
        String[] paths = path.split(",");
        List<Resources> reses = resourcesMapper.findMenuByPath(paths);
        return reses;
    }
}